define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            //选项卡A元素绑定切换事件
            $('a[data-toggle=tab]').on('shown.bs.tab', function () {
                var panel = $($(this).attr("href"));

                //判断是否有获取到选项元素的
                if (panel.length > 0) {
                    //Controller.table.receivce()
                    Controller.table[panel.attr('id')].call(this)

                    //刷新表格
                    $(this).on('click', function (e) {
                        $($(this).attr("href")).find(".btn-refresh").trigger("click");
                    });
                }

                //移除绑定的事件
                $(this).unbind('shown.bs.tab');

            })

            //必须默认触发shown.bs.tab事件 一进入就默认触发一个选项卡
            $('ul.nav-tabs li.active a[data-toggle="tab"]').trigger("shown.bs.tab");

            Controller.api.bindevent();
        },
        table: { //自定义命名 - 处理选显卡的每个选项的方法
            info() {
                // console.log('客户资料 - ajax')
                Controller.api.bindevent();
            },
            receive: function () {
                // console.log('申请记录 - ajax')

                var ids = Fast.api.query('ids');

                Table.api.init({
                    extend: {
                        index_url: `business/info/receive?ids=${ids}`,
                        table: 'business_receive',
                    }
                });

                var ReceiveTable = $("#ReceiveTable");

                ReceiveTable.bootstrapTable({
                    url: $.fn.bootstrapTable.defaults.extend.index_url,
                    toolbar: "#ReceiveToolbar", //工具栏
                    pk: 'id', //默认主键字段名
                    sortName: 'applytime', //排序的字段名
                    sortOrder: 'desc', //排序的方式
                    columns: [
                        [
                            {
                                field: 'applytime',
                                title: __('BusinessApplytime'),
                                sortable: true,
                                formatter: Table.api.formatter.datetime,
                                operate: 'RANGE',
                                addclass: 'datetimerange'
                            },
                            { field: 'admin.nickname', title: __('BusinessApply'), sortable: false, operate: 'LIKE' },
                            {
                                field: 'status',
                                title: __('Status'),
                                formatter: Table.api.formatter.status,
                                searchList: { apply: __('Apply'), allot: __('Allot'), recovery: __('Recovery') },
                                operate: 'LIKE',
                            },
                        ]
                    ]
                });

                Table.api.bindevent(ReceiveTable);
            },
            visit() {
                // console.log('回访记录 - ajax')

                //获取当前的客户ID
                var ids = Fast.api.query('ids')
                // console.log(ids)

                //初始化表格结构
                Table.api.init({
                    extend: {
                        VisitIndex_url: `business/info/visit?ids=${ids}`,
                        VisitAdd_url: `business/visit/add?ids=${ids}`,
                        VisitEdit_url: `business/visit/edit`,
                        VisitDel_url: `business/visit/del`,
                        table: 'business_visit',
                    }
                });

                //获取表格元素
                var VisitTable = $('#VisitTable')

                VisitTable.bootstrapTable({
                    url: $.fn.bootstrapTable.defaults.extend.VisitIndex_url,
                    toolbar: "#VisitToolbar", //工具栏
                    pk: 'id', //默认主键字段名
                    sortName: 'createtime', //排序的字段名
                    sortOrder: 'desc', //排序的方式
                    columns: [
                        [
                            { checkbox: true },
                            { field: 'id', title: __('Id'), sortable: true, operate: false },
                            {
                                field: 'createtime',
                                title: __('VisitTime'),
                                formatter: Table.api.formatter.datetime,
                                operate: 'RANGE',
                                addclass: 'datetimerange',
                                sortable: true
                            },
                            { field: 'admin.nickname', title: __('BusinessAdmin'), operate: 'LIKE' },
                            {
                                field: 'content',
                                title: __('VisitContent'),
                                // cellStyle: function (value, row, index) {
                                //     return {
                                //         css: {
                                //             "white-space": "nowrap",
                                //             "text-overflow": "ellipsis",
                                //             "overflow": "hidden",
                                //             "max-width": "400px"
                                //         }
                                //     }
                                // },
                                // formatter: function (value, row, index) {
                                //     if (value) {
                                //         value = value.replace(/<.*?>/g, "")

                                //     } else {
                                //         value = "暂无回访内容"
                                //     }

                                //     var span = document.createElement('span')
                                //     span.setAttribute('title', value)
                                //     span.innerHTML = value
                                //     return span.outerHTML
                                // },
                                operate: 'LIKE'
                            },
                            {
                                field: 'operate',
                                title: __('Operate'),
                                table: VisitTable,
                                events: Table.api.events.operate,
                                formatter: Table.api.formatter.operate,
                                buttons: [
                                    {
                                        name: 'VisitEdit',
                                        title: '编辑',
                                        icon: 'fa fa-pencil',
                                        classname: 'btn btn-xs btn-success btn-dialog',
                                        extend: 'data-toggle=\'tooltip\' data-area= \'["50%", "50%"]\'',
                                        url: $.fn.bootstrapTable.defaults.extend.VisitEdit_url,
                                    },
                                    {
                                        name: 'VisitDel',
                                        title: '删除',
                                        icon: 'fa fa-trash',
                                        classname: 'btn btn-xs btn-danger btn-magic btn-ajax',
                                        url: $.fn.bootstrapTable.defaults.extend.VisitDel_url,
                                        confirm: '是否确认删除？',
                                        extend: 'data-toggle=\'tooltip\' data-area= \'["100%", "100%"]\'',
                                        success: function (data, ret) {
                                            VisitTable.bootstrapTable('refresh');
                                        }
                                    },

                                ]
                            }
                        ]
                    ]
                });

                //给添加按钮绑定事件，打开一个新的窗口
                $(".btn-visit-add").on('click', function () {
                    Fast.api.open($.fn.bootstrapTable.defaults.extend.VisitAdd_url, '添加回访记录', { area: ['50%', '50%'] })
                })

                Table.api.bindevent(VisitTable);
            },
            record: function () {
                // console.log('消费记录 - ajax')

                var ids = Fast.api.query('ids');

                Table.api.init({
                    extend: {
                        index_url: `business/info/record?ids=${ids}`,
                        table: 'business_record',
                    }
                });

                var RecordTable = $("#RecordTable");

                RecordTable.bootstrapTable({
                    url: $.fn.bootstrapTable.defaults.extend.index_url,
                    toolbar: "#RecordToolbar", //工具栏
                    pk: 'id', //默认主键字段名
                    sortName: 'createtime', //排序的字段名
                    sortOrder: 'desc', //排序的方式
                    columns: [
                        [
                            {
                                field: 'createtime',
                                title: __('RecordTime'),
                                sortable: true,
                                formatter: Table.api.formatter.datetime,
                                operate: 'RANGE',
                                addclass: 'datetimerange'
                            },
                            { field: 'total', title: __('RecordTotal'), sortable: true, operate: false },
                            { field: 'content', title: __('RecordContent'), sortable: false, operate: 'LIKE' },
                        ]
                    ]
                });

                Table.api.bindevent(RecordTable);
            },
            subject: function () {
                // console.log('课程订单 - ajax')

                var ids = Fast.api.query('ids');

                Table.api.init({
                    extend: {
                        index_url: `business/info/subject?ids=${ids}`,
                        table: 'subject_order',
                    }
                });

                var SubjectTable = $("#SubjectTable");

                SubjectTable.bootstrapTable({
                    url: $.fn.bootstrapTable.defaults.extend.index_url,
                    toolbar: "#SubjectToolbar", //工具栏
                    pk: 'id', //默认主键字段名
                    sortName: 'total', //排序的字段名
                    sortOrder: 'desc', //排序的方式
                    columns: [
                        [

                            { field: 'code', title: __('SubjectCode'), sortable: false, operate: 'LIKE' },
                            { field: 'subject.title', title: __('SubjectName'), sortable: true, operate: 'LIKE' },
                            { field: 'total', title: __('SubjectTotal'), sortable: true, operate: false },
                            {
                                field: 'createtime',
                                title: __('SubjectTime'),
                                sortable: true,
                                formatter: Table.api.formatter.datetime,
                                operate: 'RANGE',
                                addclass: 'datetimerange'
                            },
                        ]
                    ]
                });

                Table.api.bindevent(SubjectTable);
            },
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
